<template>
  <div   style="width:100%">
    <el-container style="width:100%;height: 100%; border: 1px solid #eee">

      <el-container>
        <el-header style="text-align: left; font-size: 12px">
        </el-header>

        <el-main>
          <el-table
              :data="tableData"
              style="width: 100%"
              stripe
              border
              max-height="575">
            <el-table-column
                fixed
                prop="occurrenceTime"
                label="违纪发生时间"
                :formatter="formatDate">
            </el-table-column>
            <el-table-column
                prop="studentName"
                label="姓名">
            </el-table-column>
            <el-table-column
                prop="reason"
                label="原因">
            </el-table-column>
            <el-table-column
                fixed="right"
                label="操作"
                width="120">
              <template slot-scope="scope">
                <el-button type="text" size="small" @click="editForm(scope.row)">编辑</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-main>
        <el-footer>
          <div class="block">
            <el-pagination
                @current-change="handleCurrentChange"
                :current-page="pageNo"
                :page-size="pageSize"
                layout="total, prev, pager, next, jumper"
                :total=totalNum>
            </el-pagination>
          </div>
        </el-footer>
      </el-container>
    </el-container>
    <el-dialog title="违纪审核" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="申请时间" :label-width="formLabelWidth">
          {{this.form.time}}
        </el-form-item>
        <el-form-item label="姓名" :label-width="formLabelWidth">
          {{this.form.name}}
        </el-form-item>
        <el-form-item label="原因" :label-width="formLabelWidth">
          {{this.form.reason}}
        </el-form-item>
        <el-form-item label="扣分" :label-width="formLabelWidth">
          <el-input v-model="form.score" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="sendForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getIndisciplineAudit, updateIndisciplineAudit} from '../../../../api/leader'
export default {
  data () {
    return {
      pageSize: 5,
      totalNum: 0,
      pageNo: 1,
      DOT: {
        pageSize: 5,
        pageNo: 1,
        data: {
          ID: JSON.parse(localStorage.getItem('user')).id.toString()
        }
      },
      tableData: [],
      multipleSelection: [],
      dialogFormVisible: false,
      form: {
        ID: null,
        name: null,
        time: null,
        reason: null,
        score: null
      },
      formLabelWidth: '120px'
    }
  },
  methods: {
    formatDate (row) {
      const date = new Date(row.occurrenceTime)
      const beijingTime = new Date(date.getTime())
      const year = beijingTime.getFullYear()
      const month = (beijingTime.getMonth() + 1).toString().padStart(2, '0')
      const day = beijingTime.getDate().toString().padStart(2, '0')
      return `${year}年${month}月${day}日`
    },
    handleCurrentChange (newPage) {
      this.pageNo = newPage
      this.DOT.pageNo = newPage
      this.queryIndisciplineAudit()
    },
    queryIndisciplineAudit () {
      getIndisciplineAudit(this.DOT).then(res => {
        this.tableData = res.data
        this.totalNum = res.otherData.totalNum
      })
    },
    editForm (row) {
      this.dialogFormVisible = true
      this.form.ID = row.id.toString()
      this.form.name = row.studentName
      this.form.time = this.formatDate(row)
      this.form.reason = row.reason
    },
    sendForm () {
      if (this.form.score === null) {
        this.$message({
          message: '请输入扣分',
          type: 'error'
        })
        return
      } else {
        let obj = {
          ID: this.form.ID,
          score: this.form.score.toString(),
          leaderID: JSON.parse(localStorage.getItem('user')).id.toString()
        }
        updateIndisciplineAudit(obj).then(res => {
          if (res.status === true) {
            this.queryIndisciplineAudit()
          } else {
            this.$message({
              message: '失败'
            })
          }
        })
      }
      this.dialogFormVisible = false
    }
  },
  created () {
    this.queryIndisciplineAudit()
  }
}
</script>

<style scoped>
.el-header {
  background-color: #B3C0D1;
  color: #333;
  line-height: 60px;
}

.el-aside {
  color: #333;
}
</style>
